package cn.hncu.booksManagementSystem.user.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.common.enums.UserTypeEnum;
import cn.hncu.booksManagementSystem.common.enums.XxxIdEnum;
import cn.hncu.booksManagementSystem.user.business.ebi.UserEbi;
import cn.hncu.booksManagementSystem.user.business.factory.UserEbiFactory;
import cn.hncu.booksManagementSystem.user.vo.UserModel;
import cn.hncu.booksManagementSystem.utils.GeneratorXxxID_Util;

public class AddPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private JFrame mainFrame;
	private JTextField tfdID;
	private JTextField tfdName;
	private JPasswordField tfdPwd;
	
	//注入ebi
	private UserEbi ebi = UserEbiFactory.getInstanceOfUserEbi();
	/**
	 * Create the panel.
	 */
	public AddPanel(JFrame mainFrame) {
		this.mainFrame = mainFrame;
		initContentPane();
	}
	
	public void initContentPane() {
		setLayout(null);
		
		JLabel label = new JLabel("添加用户");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(312, 44, 158, 43);
		add(label);
		
		JLabel lblid = new JLabel("用户ID：");
		lblid.setForeground(Color.BLUE);
		lblid.setFont(new Font("微软雅黑", Font.BOLD, 24));
		lblid.setHorizontalAlignment(SwingConstants.CENTER);
		lblid.setBounds(68, 137, 109, 32);
		add(lblid);
		
		tfdID = new JTextField(GeneratorXxxID_Util.getXxxId(XxxIdEnum.USER));
		tfdID.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdID.setBounds(187, 137, 204, 32);
		tfdID.setEnabled(false);
		add(tfdID);
		tfdID.setColumns(10);
		
		JLabel label_1 = new JLabel("用户名称：");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setForeground(Color.BLUE);
		label_1.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_1.setBounds(68, 255, 136, 32);
		add(label_1);
		
		tfdName = new JTextField();
		tfdName.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdName.setColumns(10);
		tfdName.setBounds(203, 255, 188, 32);
		add(tfdName);
		
		JLabel label_2 = new JLabel("用户密码：");
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setForeground(Color.BLUE);
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_2.setBounds(417, 255, 136, 32);
		add(label_2);
		
		tfdPwd = new JPasswordField();
		tfdPwd.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdPwd.setBounds(547, 255, 174, 32);
		add(tfdPwd);
		
		JComboBox<String> cbbxType = new JComboBox<String> ();
		for (UserTypeEnum ute : UserTypeEnum.values()) {
			cbbxType.addItem(ute.getName());
		}
		cbbxType.setFont(new Font("微软雅黑", Font.BOLD, 24));
		cbbxType.setBounds(472, 137, 214, 32);
		add(cbbxType);
		
		JButton btnAdd = new JButton("添加");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//1收集参数 id,name,pwd,type
				String id = tfdID.getText();
				String name = tfdName.getText();
				@SuppressWarnings("deprecation")
				String pwd = tfdPwd.getText();
				if (name==null || name.trim().length()==0) {
					JOptionPane.showMessageDialog(null, "用户名不能为空！");
					return;
				}
				boolean isLegal = name.matches("^\\w{3,10}$");
				if (!isLegal) {
					JOptionPane.showMessageDialog(null, "用户名格式错误，只能由字母、数字和_组成，并且长度在3-10之间");
					return;
				}
				
				if (pwd==null|| pwd.trim().length()==0) {
					JOptionPane.showMessageDialog(null, "密码不能为空！");
					return;
				}
				isLegal = pwd.matches("^\\w{3,10}$");
				if (!isLegal) {
					JOptionPane.showMessageDialog(null, "密码格式错误，只能由字母、数字和_组成，并且长度在3-10之间");
					return;
				}
				Object obj = cbbxType.getSelectedItem();
				int type = UserTypeEnum.getTypeByName(obj.toString());
				
				//2 组织参数
				UserModel user = new UserModel();
				user.setId(id);
				user.setName(name);
				user.setPwd(pwd);
				user.setType(type);
				
				//3 调用逻辑层
				boolean boo = ebi.createUser(user);
				
				//4 返回结果界面
				if (boo) {
					mainFrame.setContentPane(new ListPanel(mainFrame));
					mainFrame.validate();
				}else {
					JOptionPane.showMessageDialog(null, "用户已存在，请重新输入");
				}
			}
		});
		btnAdd.setForeground(Color.BLUE);
		btnAdd.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnAdd.setBounds(203, 358, 147, 43);
		add(btnAdd);
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				mainFrame.setContentPane(new ListPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnBack.setForeground(Color.BLUE);
		btnBack.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnBack.setBounds(460, 358, 147, 43);
		add(btnBack);
	}
}
